define(function () {
	var exports = {};

	var tagCloud = require('dashboard/js/tagCloud'),
		util = require('dashboard/js/util');


	var initDataChart = {
		time : [],
		pdata : [],
		ndata : []
	},
	termFlag = true,
	nowTime = 0,
	nowMinData = [],
	preUsercount = 0;

	/* for map total */
	var oldCash = '';
	var oldClick = '';
    
    //  渲染echart
    var renderEchart = function (rdata, flag) {
        // echart初始化
        require([ 'echarts', 'echarts/chart/line' ], function (ec) {
            var myChart = ec.init(document.getElementById('mainChart')); 

            var option = {
			    tooltip : {
			        trigger: 'axis',
			    },
			    legend: {
			        data:['今日浏览量','昨日浏览量'],
			        x: 'right',
			        padding: [5,20,0,0],
			        textStyle: {
			        	color: '#818181'
			        }
			    },
			    grid: {
			    	borderWidth : '0',
			    	y: 70
			    },
			    toolbox: {
			        show : false
			    },
			    color: ['#ea6004','#213f51'],
			    calculable : true,
			    animation: false,
			    xAxis : [
			        {
			            type : 'category',
			            boundaryGap : false,
			            splitLine : {
			            	lineStyle : {
			            		color : '#242424'
			            	}
			            },
			            axisLine : {
			            	lineStyle : {
			            		color : '#424242'
			            	}
			            },
			            axisLabel : {
			            	textStyle : {
			            		color : '#7e7e7e'
			            	},
			            	interval: 59,
			            	formatter: function(value){
			            		return value.substr(0,2)+'点';
			            	}
			            },
			            data: function() {
			                var x = [];
			                for (var i = 0; i < 24; i++) {
			                	for (var j =0; j < 60; j++){
			                		var hour = i < 10 ? '0'+i.toString() : i.toString();
			                		var min = j <10 ? '0'+j.toString() : j.toString();
			                		x.push(hour+':'+min);
			                	}
			                }
			                return x;
			            }()
			        }
			    ],
			    yAxis : [
			        {
			            type : 'value',
			            axisLine : {
			            	lineStyle : {
			            		color : '#424242'
			            	}
			            },
			            axisLabel : {
			            	textStyle : {
			            		color : '#7e7e7e'
			            	}
			            },
			            splitLine : {
			            	lineStyle : {
			            		color : '#242424'
			            	}
			            },
			        }
			    ],
			    series : [
			        {
			            name:'今日浏览量',
			            type:'line',
			            smooth:true,
            			symbol:'none',
			            itemStyle: {
			            	normal: {
			            		areaStyle: {type:'default'}
			            	}
			            },
			            data:[],
			            markPoint: {
			            	data: [
			            	{
			                    name: '当前分钟浏览量',
			                    clickable: false,
			                    symbol:'pin',
			                    symbolSize:30,
		                    	xAxis : '',
		                    	yAxis : '',
			                    itemStyle:{
			                    	normal:{
			                        	color: '#0c0c0c',
			                    		borderColor: '#757575',
			                    		borderWidth: '1',
										label: {
			                    			textStyle: {fontSize:13,color:'#999999'},
			                    			formatter: function(mdata){
			                    				return ' 分钟统计 \n ' + mdata.value + ' ';
			                    			}
			                    		}
			                        },
			                        emphasis:{
			                        	borderWidth: '1',
			                        	label: { textStyle: {fontSize:13}}
			                        }
			                    }
			                },
			                {
			                	name: '当前点',
			                	symbol: 'circle', 
			                	symbolSize: 8,
                     			effect:{show:true,type:'bounce'},
                     			itemStyle:{normal:{color:'#e26d1e',borderColor:'#848484',borderWidth:4}}
			                }]
			            },
			        },
			        {
			            name:'昨日浏览量',
			            type:'line',
			            smooth:true,
            			symbol:'none',
			            itemStyle: {
			            	normal: {
			            		lineStyle:{
			                        width:1
			                    },
			            		areaStyle: {type:'default'}
			            	}
			            },
			            data:[]
			        }
			    ]
            };
    		
    		if (flag == 'init') {
    			var initNdata = [], initTime = [];
    			_.each(rdata, function(value,key){  
    				value.value && initNdata.push(value.value);
    				value.time && initTime.push(value.time);
    			})
    			initDataChart.ndata = initNdata;
    			initDataChart.time = initTime;
    			option.series[1].data = initDataChart.pdata;
				option.series[0].data = initNdata;
				option.series[0].markPoint.data[0].xAxis = util.timeToHour(initDataChart.time[initDataChart.time.length-1]);
				option.series[0].markPoint.data[0].yAxis = option.series[0].data[option.series[0].data.length-1]+6000;
				option.series[0].markPoint.data[0].value = option.series[0].data[option.series[0].data.length-1];
				option.series[0].markPoint.data[1].xAxis = util.timeToHour(initDataChart.time[initDataChart.time.length-1]);
				option.series[0].markPoint.data[1].yAxis = option.series[0].data[option.series[0].data.length-1];
    			// option.series[0].markPoint.data[1].value = option.series[0].data[option.series[0].data.length-1];
    			nowTime = initDataChart.time[initDataChart.time.length-1];
				var now = new Date();
				now.setTime(nowTime);
				$('#dataRealtime').html(now.toLocaleDateString().replace(/\//g,'-') + ' ' + now.toTimeString().substr(0,8));
    		} else {
    			option.series[1].data = initDataChart.pdata;
				option.series[0].data = initDataChart.ndata;
    			option.series[0].markPoint.data[0].xAxis = util.timeToHour(initDataChart.time[initDataChart.time.length-1]);
				option.series[0].markPoint.data[0].yAxis = option.series[0].data[option.series[0].data.length-1]+6000;
    			option.series[0].markPoint.data[1].xAxis = util.timeToHour(initDataChart.time[initDataChart.time.length-1]);
				option.series[0].markPoint.data[1].yAxis = option.series[0].data[option.series[0].data.length-1];
    			// option.series[0].markPoint.data[1].value = option.series[0].data[option.series[0].data.length-1];
    			if (flag == 'addpin') {
    				var sum = _.reduce(rdata, function(memo, num, key){ 
    					if (key > 17){
					 		return memo + num.value;
    					} else {
    						return 0;
    					}
    				}, 0);
    				option.series[0].markPoint.data[0].value = sum;
    				var now = new Date();
					now.setTime(rdata[rdata.length-1].time);
					$('#dataRealtime').html(now.toLocaleDateString().replace(/\//g,'-') + ' ' + now.toTimeString().substr(0,8));
    			} else {
    				option.series[0].markPoint.data[0].value = option.series[0].data[option.series[0].data.length-1];
    			}
    		}

            // 为echarts对象加载数据 
            myChart.setOption(option); 
        });
    }

    /* render map function */
    var renderMap = function (mdata){
    	require([ 'echarts', 'echarts/chart/map' ], function (ec) {
			var myChart = ec.init(document.getElementById('mainChart'));

			var geo = {"阿坝":[102.4805,32.4536],"阿克苏":[82.9797,41.0229],"阿拉尔":[81.287026,40.552106],"阿拉善盟":[105.695683,38.843075],"阿勒泰":[88.2971,47.0929],"阿里":[82.3645,32.7667],"安徽":[117.2461,32.0361],"安康":[109.1162,32.7722],"安庆":[117.058739,30.537898],"安顺":[105.92827,26.228595],"安阳":[114.5325,36.0022],"鞍山":[123.0798,40.6055],"澳门":[113.5715,22.1583],"巴南区":[106.7322,29.4214],"巴彦淖尔市":[107.5562,41.3196],"巴音郭楞":[88.1653,39.6002],"巴中":[107.0618,31.9977],"白城":[123.0029,45.2637],"白沙":[109.445689,19.23112],"白山":[127.2217,42.0941],"白银":[104.8645,36.5076],"百色":[106.624775,23.908371],"蚌埠":[117.4109,33.1073],"包头":[110.3467,41.4899],"保定":[115.49481,38.886565],"保山":[99.177996,25.120489],"保亭":[109.697221,18.647938],"宝坻区":[117.4274,39.5913],"宝鸡":[107.1826,34.3433],"宝山区":[121.4346,31.4051],"北碚区":[106.5674,29.8883],"北辰区":[117.1761,39.2548],"北海":[109.314,21.6211],"北京":[116.4551,40.2539],"本溪":[124.1455,41.1987],"毕节":[105.290492,27.308589],"璧山":[106.2048,29.5807],"滨州":[117.968292,37.405314],"亳州":[116.1914,33.4698],"博尔塔拉":[82.072378,44.905979],"沧州":[116.863806,38.297615],"昌都":[97.185582,31.140576],"昌吉":[89.6814,44.4507],"昌江":[109.0407,19.2137],"昌平区":[116.1777,40.2134],"常德":[111.653718,29.012149],"常州":[119.980151,31.815856],"长春":[125.8154,44.2584],"长葛":[113.778489,34.220256],"长宁区":[121.3852,31.2115],"长沙":[113.0823,28.2568],"长寿区":[107.1606,29.9762],"长治":[113.120292,36.201664],"巢湖":[117.88049,31.608733],"潮州":[116.7847,23.8293],"郴州":[113.2361,25.8673],"城口":[108.7756,31.9098],"成都":[104.067923,30.679943],"承德":[117.933822,40.992521],"澄迈":[110.016202,19.742046],"池州":[117.494477,30.660019],"赤峰":[118.930761,42.297112],"崇明":[121.5637,31.5383],"崇文区":[116.4166,39.8811],"崇左":[107.380156,22.380782],"滁州":[118.32457,32.317351],"楚雄":[101.6016,25.3619],"达州":[107.6111,31.333],"大渡口区":[106.4905,29.4214],"大丰":[120.546274,33.190868],"大港区":[117.3875,38.757],"大理":[99.9536,25.6805],"大连":[122.2229,39.4409],"大庆":[125.02184,46.596709],"大同":[113.7854,39.8035],"大兴安岭":[124.317289,51.992308],"大兴区":[116.4716,39.6352],"大足":[105.7544,29.6136],"丹东":[124.541,40.4242],"儋州":[109.57447,19.521401],"德宏":[98.589434,24.44124],"德阳":[104.48,31.1133],"德州":[116.6858,37.2107],"迪庆":[99.713682,27.831029],"垫江":[107.4573,30.2454],"定安":[110.357171,19.68107],"定西":[104.626638,35.586056],"定州":[114.996919,38.522176],"东城区":[116.418,39.9367],"东方":[108.8498,19.0414],"东莞":[113.8953,22.901],"东丽区":[117.4013,39.1223],"东营":[118.7073,37.5513],"鄂尔多斯":[109.787827,39.614266],"鄂州":[114.900228,30.397737],"恩施":[109.5007,30.2563],"房山区":[115.8453,39.7163],"防城港":[108.351791,21.617398],"丰都":[107.8418,29.9048],"丰台区":[116.2683,39.8309],"奉节":[109.3909,30.9265],"奉贤区":[121.5747,30.8475],"佛山":[113.134026,23.035095],"涪陵区":[107.3364,29.6796],"福建":[118.3008,25.9277],"福州":[119.4543,25.9222],"抚顺":[124.585,41.8579],"抚州":[116.4441,27.4933],"莆田":[119.0918,25.3455],"阜新":[122.0032,42.2699],"阜阳":[115.820932,32.901211],"甘南":[102.9199,34.6893],"甘肃":[95.7129,40.166],"甘孜":[99.9207,31.0803],"赣州":[115.2795,25.8124],"菏泽":[115.6201,35.2057],"固原":[106.285268,36.021523],"馆陶":[115.300227,36.54843],"广安":[106.639712,30.461962],"广东":[113.4668,22.8076],"广西":[108.2813,23.6426],"广元":[105.850014,32.439212],"广州":[113.5107,23.2196],"桂林":[110.5554,25.318],"贵港":[109.9402,23.3459],"贵阳":[106.6992,26.7682],"贵州":[106.6113,26.9385],"果洛":[99.3823,34.0466],"哈尔滨":[127.9688,45.368],"哈密":[93.7793,42.9236],"海北":[100.907685,36.960539],"海淀区":[116.2202,40.0239],"海东":[102.3706,36.2988],"海口":[110.3893,19.8516],"海拉尔":[119.743282,49.218547],"海南":[100.625284,36.283118],"海南省":[109.9512,19.2041],"海西":[97.377407,37.350853],"邯郸":[114.482694,36.609308],"汉沽区":[117.8888,39.2191],"汉中":[107.045478,33.081569],"杭州":[120.219375,30.259244],"合川区":[106.3257,30.108],"合肥":[117.29,32.0581],"和平区":[117.2008,39.1189],"和田":[81.167,36.9855],"和县":[118.360177,31.747983],"河北":[115.4004,37.9688],"河北区":[117.2145,39.1615],"河池":[108.069948,24.699521],"河东区":[117.2571,39.1209],"河南":[113.4668,33.8818],"河西区":[117.2365,39.0804],"河源":[114.917,23.9722],"贺州":[111.552594,24.411054],"鹤壁":[114.3787,35.744],"鹤岗":[130.4407,47.7081],"黑河":[127.53496,50.251297],"黑龙江":[128.1445,48.5156],"衡水":[115.8838,37.7161],"衡阳":[112.583819,26.898164],"红河":[103.384065,23.367718],"红桥区":[117.1596,39.1663],"虹口区":[121.4882,31.2788],"呼和浩特":[111.660351,40.828319],"呼伦贝尔":[120.8057,50.2185],"湖北":[112.2363,31.1572],"湖南":[111.5332,27.3779],"湖州":[120.137243,30.877925],"葫芦岛":[120.843977,40.716869],"怀化":[109.986959,27.557483],"怀柔区":[116.6377,40.6219],"淮安":[119.030186,33.606513],"淮北":[116.791447,33.960023],"淮南":[117.018639,32.642812],"黄冈":[115.2686,30.6628],"黄南":[102.025169,35.525683],"黄浦区":[121.4868,31.219],"黄山":[118.321411,29.712904],"黄石":[115.050683,30.216127],"惠州":[114.6204,23.1647],"霍邱":[116.284609,32.359549],"鸡西":[132.7917,45.7361],"吉安":[114.992039,27.113848],"吉林":[126.8372,43.6047],"吉林省":[126.4746,43.5938],"济南":[117.1582,36.8701],"济宁":[116.8286,35.3375],"济源":[112.405268,35.105365],"蓟":[117.4672,40.004],"佳木斯":[133.0005,47.5763],"嘉定区":[121.2437,31.3625],"嘉兴":[120.9155,30.6354],"嘉峪关":[98.281635,39.802397],"江北区":[106.8311,29.6191],"江津区":[106.2158,28.9874],"江门":[113.078125,22.575117],"江苏":[120.0586,32.915],"江西":[116.0156,27.29],"焦作":[113.211836,35.234608],"揭阳":[116.379501,23.547999],"津南区":[117.3958,38.9603],"金昌":[102.208126,38.516072],"金华":[120.0037,29.1028],"金山区":[121.2657,30.8112],"锦州":[121.6626,41.4294],"晋城":[112.867333,35.499834],"晋中":[112.7747,37.37],"荆门":[112.6758,30.9979],"荆州":[113.291,30.0092],"景德镇":[117.334,29.3225],"静安区":[121.4484,31.2286],"静海":[116.9824,38.8312],"九江":[116.008451,29.711103],"九龙坡区":[106.3586,29.4049],"酒泉":[98.508415,39.741474],"喀什":[77.168,37.8534],"开":[108.4131,31.2561],"开封":[114.5764,34.6124],"克拉玛依":[85.2869,45.5054],"克孜勒苏":[76.135839,39.748238],"库尔勒":[86.181159,41.731954],"昆明":[102.9199,25.4663],"拉萨":[91.1865,30.1465],"来宾":[109.7095,23.8403],"莱芜":[117.684667,36.233654],"兰州":[103.823305,36.064226],"廊坊":[116.716106,39.528296],"乐东":[109.179793,18.752425],"乐清":[120.991885,28.118824],"乐山":[103.760824,29.600958],"漯河":[114.046061,33.576279],"丽江":[100.448,26.955],"丽水":[119.929576,28.4563],"连云港":[119.173872,34.601549],"凉山":[102.259591,27.892393],"梁平":[107.7429,30.6519],"聊城":[115.986869,36.455829],"辽宁":[122.3438,41.0889],"辽阳":[123.4094,41.1383],"辽源":[125.343,42.7643],"临沧":[99.613,24.0546],"临汾":[111.538788,36.099745],"临高":[109.696356,19.913613],"临夏":[103.2715,35.5737],"临猗":[110.786628,35.14855],"临沂":[118.340768,35.072409],"林芝":[95.4602,29.1138],"陵水":[110.026652,18.510882],"柳州":[109.422402,24.329053],"六安":[116.512193,31.751011],"六盘水":[104.852087,26.591866],"龙岩":[117.017997,25.078685],"陇南":[105.304,33.5632],"娄底":[111.996396,27.741073],"卢湾区":[121.4758,31.2074],"泸州":[105.4578,28.493],"洛阳":[112.447525,34.657368],"吕梁":[111.3574,37.7325],"马鞍山":[118.6304,31.5363],"茂名":[111.0059,22.0221],"梅州":[116.126403,24.304571],"眉山":[103.84143,30.061115],"门头沟区":[115.8,39.9957],"密云":[117.0923,40.5121],"绵阳":[104.7327,31.8713],"闵行区":[121.4992,31.0838],"明港":[114.062886,32.472391],"牡丹江":[129.7815,44.7089],"那曲":[92.067018,31.48068],"南岸区":[106.6663,29.5367],"南昌":[116.0046,28.6633],"南充":[106.2048,31.1517],"南川区":[107.1716,29.1302],"南汇区":[121.8755,30.954],"南京":[118.8062,31.9208],"南开区":[117.1527,39.1065],"南宁":[108.479,23.1152],"南平":[118.184208,26.647769],"南通":[121.1023,32.1625],"南阳":[112.535006,32.996696],"内江":[105.064933,29.586229],"内蒙古":[117.5977,44.3408],"宁波":[121.5967,29.6466],"宁德":[119.6521,26.9824],"宁河":[117.6801,39.3853],"宁夏":[105.9961,37.3096],"怒江":[99.1516,26.5594],"攀枝花":[101.72695,26.585051],"盘锦":[122.073228,41.141248],"彭水":[108.2043,29.3994],"平顶山":[113.199314,33.77219],"平谷区":[117.1706,40.2052],"平凉":[107.0728,35.321],"萍乡":[113.9282,27.4823],"濮阳":[115.1917,35.799],"普洱":[100.980058,22.788778],"普陀区":[121.3879,31.2602],"浦东新区":[121.6928,31.2561],"七台河":[131.2756,45.9558],"綦江":[106.6553,28.8171],"齐齐哈尔":[124.541,47.5818],"潜江":[112.904393,30.408658],"黔东南":[108.4241,26.4166],"黔江区":[108.7207,29.4708],"黔南":[107.523205,26.264536],"黔西南":[105.5347,25.3949],"钦州":[109.0283,22.0935],"秦皇岛":[119.604368,39.945462],"清徐":[112.365165,37.613217],"清远":[113.040773,23.698469],"青岛":[120.4651,36.3373],"青海":[96.2402,35.4199],"青浦区":[121.1751,31.1909],"庆阳":[107.644227,35.726801],"琼海":[110.478013,19.261083],"琼中":[109.84583,19.039041],"衢州":[118.875842,28.95691],"曲靖":[103.9417,25.7025],"泉州":[118.600362,24.901652],"日喀则":[88.887083,29.27287],"日照":[119.50718,35.420225],"荣昌":[105.5127,29.4708],"瑞安":[120.661512,27.783731],"三门峡":[111.20673,34.778424],"三明":[117.645178,26.269407],"三沙":[112.348842,16.85613],"三亚":[109.518409,18.258847],"沙坪坝区":[106.3696,29.6191],"厦门":[118.1689,24.6478],"山东":[118.7402,36.4307],"山南":[92.2083,28.3392],"山西":[112.4121,37.6611],"陕西":[109.5996,35.6396],"汕头":[117.1692,23.3405],"汕尾":[115.5762,23.0438],"商洛":[109.934208,33.873907],"商丘":[115.741,34.2828],"上海":[121.487899,31.249162],"上饶":[117.955464,28.457623],"韶关":[113.7964,24.7028],"绍兴":[120.592467,30.002365],"邵阳":[111.461525,27.236811],"深圳":[114.5435,22.5439],"神农架":[110.579402,31.341697],"沈阳":[123.432791,41.808645],"十堰":[110.804491,32.635397],"石河子":[86.086997,44.312119],"石家庄":[114.521193,38.048136],"石景山区":[116.1887,39.9346],"石柱土家族":[108.2813,30.1025],"石嘴山":[106.4795,39.0015],"沭阳":[118.808738,34.116604],"双桥区":[105.7874,29.4928],"双鸭山":[133.5938,46.7523],"顺德":[113.18703,22.84851],"顺义区":[116.7242,40.1619],"朔州":[113.0713,39.6991],"四川":[102.9199,30.1904],"四平":[124.541,43.4894],"松江区":[121.1984,31.0268],"松原":[124.832995,45.136049],"苏州":[120.6519,31.3989],"宿迁":[118.5535,33.7775],"宿州":[117.5208,33.6841],"绥化":[126.989095,46.646064],"遂宁":[105.599742,30.538581],"随州":[113.4338,31.8768],"塔城":[86.6272,45.8514],"台山":[112.800303,22.25788],"台湾":[121.0295,23.6082],"台州":[121.440613,28.668283],"太原":[112.550864,37.890277],"泰安":[117.089415,36.188078],"泰州":[120.0586,32.5525],"唐山":[118.4766,39.6826],"塘沽区":[117.6801,38.9987],"天津":[117.4219,39.4189],"天门":[113.172796,30.664472],"天水":[105.736932,34.584319],"铁岭":[124.2773,42.7423],"通化":[125.9583,41.8579],"通辽":[122.260363,43.633756],"通州区":[116.7297,39.8131],"桐城":[116.978977,31.04115],"潼南":[105.7764,30.1135],"铜川":[109.0393,35.1947],"铜梁":[106.0291,29.8059],"铜陵":[117.9382,30.9375],"铜仁":[109.196098,27.737515],"图木舒克":[79.1345,39.8749],"吐鲁番":[89.6375,42.4127],"屯昌":[110.11052,19.357222],"瓦房店":[121.986241,39.632532],"万盛区":[106.908,28.9325],"万宁":[110.391377,18.805356],"万州区":[108.3911,30.6958],"威海":[122.093958,37.528787],"潍坊":[119.142634,36.716115],"渭南":[109.7864,35.0299],"温州":[120.690635,28.002838],"文昌":[110.8905,19.7823],"文山":[104.8865,23.5712],"乌海":[106.886,39.4739],"乌兰察布":[113.112846,41.022363],"乌鲁木齐":[87.9236,43.5883],"巫山":[109.8853,31.1188],"巫溪":[109.3359,31.4813],"吴忠":[106.853,37.3755],"无锡":[120.3442,31.5527],"梧州":[111.305472,23.485395],"芜湖":[118.384108,31.36602],"五家渠":[87.550452,44.171053],"五指山":[109.5282,18.8299],"武汉":[114.3896,30.6628],"武隆":[107.655,29.35],"武清区":[117.0621,39.4121],"武威":[103.0188,38.1061],"武夷山":[118.035258,27.754912],"西安":[109.1162,34.2004],"西藏":[88.7695,31.6846],"西城区":[116.3631,39.9353],"西宁":[101.784728,36.623144],"西青区":[117.1829,39.0022],"西双版纳":[100.8984,21.8628],"锡林郭勒":[116.082893,43.951981],"仙桃":[113.461066,30.368175],"咸宁":[114.300061,29.880657],"咸阳":[108.707509,34.345373],"湘潭":[112.935556,27.835095],"湘西":[109.7864,28.6743],"襄阳":[112.129331,32.014578],"香港":[114.2784,22.3057],"孝感":[113.9502,31.1188],"忻州":[112.727939,38.461031],"新疆":[84.9023,41.748],"新乡":[114.2029,35.3595],"新余":[114.95,27.8174],"信阳":[114.8291,32.0197],"兴安盟":[122.048167,46.083757],"邢台":[114.8071,37.2821],"秀山":[109.0173,28.5205],"徐汇区":[121.4333,31.1607],"徐州":[117.5208,34.3268],"许昌":[113.835312,34.02674],"宣城":[118.8062,30.6244],"宣武区":[116.3603,39.8852],"雅安":[103.019506,29.986086],"烟台":[121.454395,37.46985],"鄢陵":[114.186678,34.106922],"延安":[109.50051,36.60332],"延边":[129.485902,42.896414],"延庆":[116.1543,40.5286],"盐城":[120.2234,33.5577],"扬州":[119.4653,32.8162],"杨浦区":[121.528,31.2966],"阳春":[111.798066,22.176017],"阳江":[111.97701,21.871517],"阳泉":[113.569238,37.869529],"伊春":[129.1992,47.9608],"伊犁":[82.5513,43.5498],"宜宾":[104.6558,28.548],"宜昌":[111.310981,30.732758],"宜春":[115.0159,28.3228],"益阳":[112.366547,28.588088],"银川":[106.3586,38.1775],"鹰潭":[117.0813,28.2349],"营口":[122.4316,40.4297],"永川区":[105.8643,29.2566],"永新":[114.248694,26.951217],"永州":[111.709,25.752],"酉阳":[108.8196,28.8666],"榆林":[109.8743,38.205],"渝北区":[106.7212,29.8499],"渝中区":[106.5344,29.5477],"禹州":[113.494167,34.146309],"玉林":[110.2148,22.3792],"玉树":[97.013316,33.00624],"玉溪":[102.545068,24.370447],"岳阳":[113.2361,29.1357],"云浮":[112.050946,22.937976],"云南":[101.8652,25.1807],"云阳":[108.8306,31.0089],"运城":[111.1487,35.2002],"枣庄":[117.323,34.8926],"闸北区":[121.4511,31.2794],"湛江":[110.365067,21.257463],"张北":[114.721248,41.149331],"张家界":[110.5115,29.328],"张家口":[115.1477,40.8527],"张掖":[99.7998,38.7433],"漳州":[117.676205,24.517065],"章丘":[117.479345,36.744883],"昭通":[104.0955,27.6031],"朝阳":[120.457235,41.579473],"朝阳区":[116.4977,39.949],"肇庆":[112.479653,23.078663],"赵县":[114.834938,37.769612],"浙江":[120.498,29.0918],"镇江":[119.4763,31.9702],"正定":[114.577675,38.150953],"郑州":[113.649644,34.75661],"中山":[113.4229,22.478],"中卫":[105.4028,36.9525],"忠":[107.8967,30.3223],"重庆":[107.7539,30.1904],"周口":[114.873,33.6951],"舟山":[122.2559,30.2234],"株洲":[113.5327,27.0319],"珠海":[113.7305,22.1155],"驻马店":[114.1589,32.9041],"庄河":[122.972664,39.686329],"淄博":[118.059134,36.804685],"资阳":[104.9744,30.1575],"自贡":[104.776071,29.359157],"遵义":[106.93126,27.699961],"义乌":[115.0159,28.3228]};

			var option = {
		        backgroundColor: '#202020',
		        color: [
		            '#ff4700',
		            '#bd3a08',
		            '#ff4700'
		        ],
		        title: {
		            // text: '@LBG产品数据部',
		            //subtext: '纯属虚构',
		            x: 'right',
		            textStyle: {
		                color: '#858585',
		                fontSize: 14
		            }
		        },
		        legend: {
		            show:false,
		            orient: 'vertical',
		            x: 'left',
		            data: ['直营城市', '渠道城市'],
		            textStyle: {
		                color: '#fff'
		            }
		        },
		        series: [{
		            name: '直营城市',
		            type: 'map',
		            mapType: 'china',
		            hoverable: false,
		            itemStyle: {
		                normal: {
		                    borderColor: '#3a3c41',
		                    borderWidth: 1.5,
		                    areaStyle: {
		                        color: '#1b1b1b'
		                    }
		                }
		            },
		            data: [],
		            markPoint: {
		                symbolSize: 6,
		                large: true,
		                effect: {
		                    symbol: 'diamond',
		                    symbolSize: 6,
		                    show: true,
		                    period: 3,
		                    scaleSize: 5
		                },
		                data: []
		            }
		        }, {
		            name: '渠道城市',
		            type: 'map',
		            mapType: 'china',
		            hoverable: false,
		            data: [],
		            markPoint: {
		                symbolSize: 6,
		                large: true,
		                effect: {
		                    symbol: 'diamond',
		                    symbolSize: 6,
		                    show: true,
		                    period: 3,
		                    scaleSize: 5
		                },
		                data: []
		            }
		        }]
		    };

		    var re = [];
		    $.each(mdata.city_direct, function(index, value) {
                if (value.city == '全国') return;
                re.push({
                    name: value.city,
                    value: value.value,
                    geoCoord: geo[value.city]
                });
                if (!geo[value.city]) console.log(value.city);
            });
            if (re && re.length > 0) {
		    	option.series[0].markPoint.data = re;
            }
            var re2 = [];
            $.each(mdata.city_channel, function(index, value) {
                if (value.city == '全国') return;
                re2.push({
                    name: value.city,
                    value: value.value,
                    geoCoord: geo[value.city]
                });
                if (!geo[value.city]) console.log(value.city);
            });
            if (re2 && re2.length > 0) {
                option.series[1].markPoint.data = re2;
            }
            var now = new Date();
			now.setTime(mdata.time);
			$('#dataRealtime').html(now.toLocaleDateString().replace(/\//g,'-') + ' ' + now.toTimeString().substr(0,8));
		    
		    myChart.setOption(option, true);


		})
    }
    
    /* render map total */	
    var renderTotal = function (mdata) {
    	// 向上滚动
		function toUp(dom,oldStr,curStr){
			// 第一次初始化 或 当长度变化时，改变布局
			if(!oldStr  || oldStr.length != curStr.length){
				dom.html(function(){
					var html = '';

					for(var i = 0; i < curStr.length; i++){
						html += '<p><span>'+curStr.charAt(i)
								+'</span><span>'+curStr.charAt(i)+'</span></p>'
					}

					return html;
				});

				setLayout(dom);
			}

			var len = Math.max(oldStr.length,curStr.length);

			for(var i = 0; i < len;i++){

				(function(index){
					var oldStrChar = oldStr.charAt(index);
					var curStrChar = curStr.charAt(index);

					if(oldStrChar != curStrChar){

						var duration = 400   // 运动时间
						var domP = dom.children().eq(index).children(); 
						var topNum = domP.first();	// 上面一排数值
						var btmNum = domP.last();	// 下面一排数值

						// 赋值新数据
						btmNum.text(curStrChar);

						topNum.animate({
							top:  '-64px'
						},duration,function(){
							//赋值新数值
							topNum.text(curStrChar);
							topNum.css('top',0);
						});

						btmNum.animate({
							top: 0
						},duration,function(){
							btmNum.css('top','64px');
						});
					}
				})(i);
			}
		}
		// 每3数字加逗号
		function format(string){
			if (typeof string === 'number'){
				var str = parseInt(string,10) + '';
			}else if (typeof string === 'string'){
				var str = string;
			}

			var hearNum = str.length%3;
			var arr = [];

			if(hearNum != 0){
				arr.push(str.substr(0,hearNum));
			}

			for(var i = 1; i <= str.length/3;i++){
				arr.push(str.substr(hearNum + 3*(i-1),3));
			}

			return arr.join(',');
		}
		// 变成2位数
		function toDouble(num){
			return parseInt(num,10) >= 10 ? '' + num : '0' + num;
		}
		// 将浮动布局转换为绝对定位
		function setLayout(dom){

			// 先设置 top / left 
			dom.children().each(function(i){
				$(this).children().each(function(){
					$(this).css({
						top: $(this).position().top,
						left: $(this).position().left
					});
				});
			});

			// 再进行绝对定位，保证所有元素都在正确位置
			dom.children().each(function(){
				$(this).children().each(function(){
					$(this).css({
						position: 'absolute'
					});
				});
			});
		}

		var date = new Date();
		date.setTime(mdata.time);
		var curDate = date.getFullYear() + '年' + toDouble((date.getMonth() + 1)) + '月' + toDouble(date.getDate()) + '日';
		var curTime = toDouble(date.getHours()) + ':' + toDouble(date.getMinutes()) + ':' + toDouble(date.getSeconds());
		var clickPv = mdata.click_pv;

		$('.cur_date').text(curDate);

		toUp($('.money_num'),oldCash,format(mdata.click_cash))
		toUp($('.click_num'),oldClick,format(mdata.click_pv))


		// 存上一次的数据
		oldCash = format(mdata.click_cash);
		oldClick = format(mdata.click_pv);

    }

    var dealYesterday = function (data) {
    	return _.map(data, function(value,key){ 
    		return value.value;
    	})
    }


	var init = function () {

		/* aside - tagclouds */
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/netflow/getSearchWrodTopList',
            jsonp: "callback",
            jsonpCallback:"jsonpCallback",
            dataType: "json",
            async:false,
			data:{platform:'PC',num:30},
			success: function(json){ 
				termFlag = true;
				tagCloud.init({id : "tagscloud", flag: 'init', data : json.data});
			}
		})
		
		/* aside - release number */
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/tv/info/usercount',
            jsonp: "callback",
            jsonpCallback: "jsonpCallback",
            dataType: "json",
            async:false,
			success: function(json){
				// json = json.responseText;
				var temp = _.template($('#templateRelease').text());
				$('#dataRelease').html(temp({ pdata : util.numFormat(json), ndata : util.numFormat(json)}));
				preUsercount = parseInt(json);
			} 
		})

		/* content - chart */
		/*
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/netflow/getNetFlowByMinYestoday',
            jsonp: "callback",
            jsonpCallback: "jsonpCallback",
            dataType: "json",
            data: {type:'vppv'},
            async: false,
			success: function(json){
               	// json = JSON.parse(json.responseText);
               	initDataChart.pdata = dealYesterday(json.data);
               	$.ajax({
					type: 'post',
					url: '/dashboard/realtime/netflow/getNetFlowByMin',
					jsonp: "callback",
					jsonpCallback:"jsonpCallback",
					dataType: "json",
					data:{type:'vppv'},
					async:false,
					success: function(json){ 
						renderEchart(json.data,'init');
					}
				})
            } 
		})
		*/
		/* render map */
		$.ajax({
            url: '/dashboard/realtime/cpc/map/getMapData',
            type: 'POST',
            data: {},
            dataType: 'json',
            success: function(data) {
            	renderMap(data);
            }
        })
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/cpc/map/getMapDataTotal',
			async:false,
			success: function(data){
				var fadeFlash = function(){$('#mainTable').animate({'top':'-13rem'}, { duration: 2000}).animate({'top':'-13rem'}, { duration: 8000}).animate({'top':'0'}, { duration: 2000}).animate({'top':'0'}, { duration: 8000})};
				setInterval(fadeFlash,10000);
				var totalStr = '<div class="total_frame"><div class="total"><div class="date"><h3>CPC今日累计</h3></div><div class="money"><p class="money_titel">金额<small> /元</small></p><div class="money_num clearfix"></div></div><div class="click"><p class="click_titel">点击量</p><p class="click_num"></p></div></div></div>';
				$('#mainChart').after(totalStr);
				renderTotal(data);
			}
		})

		/* content - table */
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/tv/info/infocount',
            jsonp: "callback",
            jsonpCallback:"jsonpCallback",
            dataType: "json",
            data: {num:4},
            async:false,
			success: function(json){
				// json = JSON.parse(json.responseText);
				var temp = _.template($('#templateTable').text());
				$('#mainTable').html(temp({ data : json, util : util}));
			}
		})
	}

	var refreshBy2 = function () {

		/* aside - release number */
		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/tv/info/usercount',
            jsonp: "callback",
            jsonpCallback:"jsonpCallback",
            dataType: "json",
            async:false,
			success: function(json){
				// json = json.responseText;
				var temp = _.template($('#templateRelease').text());
				$('#dataRelease').html(temp({ pdata : util.numFormat(preUsercount), ndata : util.numFormat(json)}));
				preUsercount = parseInt(json);
			}
		})

		/* content - chart */
		var theDate = new Date();
		theDate.setTime(nowTime);
		/*
		if ( theDate.getHours() == 0 && theDate.getMinutes() == 0 && (theDate.getSeconds() == 0 || theDate.getSeconds() == 1) ) {
			initDataChart.time = [];
			initDataChart.pdata = [];
			initDataChart.ndata = [];
			$.ajax({
				type: 'post',
				url: '/dashboard/realtime/netflow/getNetFlowByMinYestoday',
	            jsonp: "callback",
	            jsonpCallback: "jsonpCallback",
	            dataType: "json",
	            data: {type:'vppv'},
	            async: false,
				success: function(json){
	               	// json = JSON.parse(json.responseText);
	               	initDataChart.pdata = dealYesterday(json.data);
	               	$.ajax({
						type: 'post',
						url: '/dashboard/realtime/netflow/getNetFlowByMin',
						jsonp: "callback",
						jsonpCallback:"jsonpCallback",
						dataType: "json",
						data:{type:'vppv'},
						async:false,
						success: function(json){ 
							renderEchart(json.data,'init');
						}
					})
	            } 
			})
		} else {
			$.ajax({
				type: 'post',
				url: '/dashboard/realtime/netflow/getHourData',
	            jsonp: "callback",
	            jsonpCallback: "jsonpCallback",
	            dataType: "json",
	            data: {type:'vppv', time: theDate.getTime()},
	            async: false,
				success: function(json){ 
					var differ = (json.data[json.data.length-1].time - theDate.getTime())/1000;
					if (differ >= 120) {
						$.ajax({
							type: 'post',
							url: '/dashboard/realtime/netflow/getNetFlowByMin',
				            jsonp: "callback",
				            jsonpCallback:"jsonpCallback",
				            dataType: "json",
				            data:{type:'vppv',time:nowTime},
				            async:false,
							success: function(data){ 
								initDataChart.time.push(data.data[data.data.length-1].time);
								initDataChart.ndata.push(data.data[data.data.length-1].value);
								renderEchart(initDataChart,'refresh');
							}
						})
						nowTime += 60000;
					} else {
						renderEchart(json.data,'addpin');
					}
				},
	            error: function(json){ }
	        })
	    }
	    */
	   	/* render map */
	    $.ajax({
            url: '/dashboard/realtime/cpc/map/getMapData',
            type: 'POST',
            data: {},
            dataType: 'json',
            success: function(data) {
            	renderMap(data);
            }
        })
        $.ajax({
			type: 'post',
			url: '/dashboard/realtime/cpc/map/getMapDataTotal',
			async:false,
			success: function(data){
				renderTotal(data);
			}
		})

		$.ajax({
			type: 'post',
			url: '/dashboard/realtime/tv/info/infocount',
            jsonp: "callback",
            jsonpCallback:"jsonpCallback",
            dataType: "json",
            data: {num:4},
            async:false,
			success: function(json){
				// json = JSON.parse(json.responseText);
				var temp = _.template($('#templateTable').text());
				$('#mainTable').html(temp({ data : json, util : util}));
			}
		})
	}

	var refreshBy7 = function () {
		if ( termFlag == true ) {
			/* aside - mobileTagClouds */
			$.ajax({
				type: 'post',
				url: '/dashboard/realtime/netflow/getSearchWrodTopList',
	            jsonp: "callback",
	            jsonpCallback:"jsonpCallback",
	            dataType: "json",
	            async:false,
				data:{platform:'M',num:30},
				success: function(json){
					$('.pc-title').removeClass('hightlight');
					$('.m-title').addClass('hightlight');
					tagCloud.init({id : "tagscloud", flag: 'refresh', data : json.data})();
					termFlag = false;
				}
			})
		} else {
			/* aside - mobileTagClouds */
			$.ajax({
				type: 'post',
				url: '/dashboard/realtime/netflow/getSearchWrodTopList',
	            jsonp: "callback",
	            jsonpCallback:"jsonpCallback",
	            dataType: "json",
	            async:false,
				data:{platform:'PC',num:30},
				success: function(json){
					$('.pc-title').addClass('hightlight');
					$('.m-title').removeClass('hightlight');
					// $('.hotwords-frame h3').fadeOut(500);
					// setTimeout(function(){
					// 	$('.hotwords-frame h3').text('PC热词');
					// 	$('.hotwords-frame h3').fadeIn(500);
					// },500)
					tagCloud.init({id : "tagscloud", flag: 'refresh', data : json.data})();
					termFlag = true;
				}
			})
		}
	}

	exports.render = function () {
		init();
		clearInterval(timeTicket2);
		var timeTicket2 = setInterval(refreshBy2, 2000);
		clearInterval(timeTicket7);
		var timeTicket7 = setInterval(refreshBy7, 7000);
	}
	return {
		render:function () {
			init();
			clearInterval(timeTicket2);
			var timeTicket2 = setInterval(refreshBy2, 2000);
			clearInterval(timeTicket7);
			var timeTicket7 = setInterval(refreshBy7, 7000);
		}
	};
})